package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐：
     * 1. 对应分类下的菜品信息查询出来
     * 2. 提交新增：
     * 1. 新增套餐基本数据 (setmeal)
     * 2. 新增套餐对应的菜品数据（setmeal_dish）
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addSetcmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> pageSetmeal(SetmealPageQueryDTO setmealPage) {
        log.info("套餐分页查询:{}", setmealPage);
        PageResult pageResult = setmealService.pageSetmeal(setmealPage);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//清除setmealCache下的所有缓存
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    @ApiOperation("套餐分页查询")
    @CacheEvict(value = "setmealCache",allEntries = true)//清除setmealCache下的所有缓存
    public Result setmealDeletes(@RequestParam  List<Long> ids) {
        setmealService.setmealDeletes(ids);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售,停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,Long setmealId) {
        setmealService.updateStatus(status,setmealId);
        return Result.success();
    }
}
